﻿var allbranchDetail;

jQuery(document).ready(function () {
    getAllDraftPi();
    getAllExportPi();
});

function getAllDraftPi() {
   
    jQuery.ajax({
        type: "Get",
        url: "/api/ProductApi/GetAllDraftPi",
        contentType: "application/json",
        success: function (result) {
            allbranchDetail = result;
            var allbranch = [];
            jQuery.each(result, function (i,d) {
                var branch = {
                    mbranchno: jQuery.trim(d.LocationNo),
                    mprovdesc: jQuery.trim(d.LocationName),
                    mprovtrans: jQuery.trim(d.Location),
                    mprovtrans1: jQuery.trim(d.Location),
                    mprovtrans2: jQuery.trim(d.Location),
                }
                allbranch.push(branch);
            });
            jQuery('#DraftAll').datagrid({
                data: allbranch
            });
        },
        error: function (data) {
            if (func) {
                func(data);
            }
        }
    });
}

function getAllExportPi() {

    jQuery.ajax({
        type: "Get",
        url: "/api/ProductApi/GetAllExportPi",
        contentType: "application/json",
        success: function (result) {
            jQuery('#ExportAll').datagrid({
                data: result
            });
        },
        error: function (data) {
            if (func) {
                func(data);
            }
        }
    });

}

function openDraftDetail(branchCode) {

    jQuery('#draftDetails').dialog('open');
    var branchDetail = jQuery.grep(allbranchDetail, function (d) {
        return jQuery.trim(d.Location) == branchCode;
    });
    if (branchDetail != null) {
        var draftDetail = branchDetail[0].DraftPi;
        jQuery('#DraftDetails').datagrid({
            data: draftDetail
        });
    }
}

function createExport(branchCode) {

    jQuery.ajax({
        type: "Get",
        url: "/api/ProductApi/GetCreateExportPi",
        data: { "branchCode": branchCode },
        contentType: "application/json",
        success: function (result) {
            if (result == "true" || result == true) {
                alert("Create Export Success");
                getAllExportPi();
            }
            else {
                alert("Cannot create export");
            }
        },
        error: function (data) {
            if (func) {
                func(data);
            }
        }
    });
}

function formatViewDraft(val, data, index) {
    if (data.mprovtrans === null || data.mprovtrans === '') {
        return "";
    }
    else {
        var productCode = jQuery.trim(data.mprovtrans);
        return '<a href="#" onclick=javascript:openDraftDetail("' + productCode + '");>View</a>';

    }
}

function formatCreateExport(val, data, index) {
    if (data.mprovtrans === null || data.mprovtrans === '') {
        return "";
    }
    else {
        var productCode = jQuery.trim(data.mprovtrans);
        return '<a href="#" onclick=javascript:createExport("' + productCode + '");>Create</a>';
    }
}

function exportPi(exportId, func) {
    if (exportId != '') {
        window.open("/api/ProductApi/GetDownloadExport?exportId=" + exportId, "_blank", "toolbar=no, scrollbars=no, resizable=yes, top=500, left=500, width=400, height=400");
    }
    else {
        alert("กรุณาระบุเลือกรายการที่ต้องการ export");
    }
}

function formatExportPI(val, data, index) {
    if (val === null || val === '') {
        return "";
    }
    else {
        return '<a href="#" onclick=javascript:exportPi("' + val + '");>Create</a>';
    }
}